﻿namespace IBTrader.Modules.Read
{
    using AxTWSLib;
    using IBTrader.Modules.Configuration;
    using IBTrader.Modules.Market;
    using NLog;
    using System.Threading;

    class ReaderRealTime : Reader
    {
        private static readonly Logger logger = LogManager.GetCurrentClassLogger();
        private readonly AutoResetEvent cancel = new AutoResetEvent(false);
        public override void Read(object _, _DTwsEvents_tickPriceEvent e)
        {
            if (configuration == null) return;
            Add((TickType)e.tickType, Times.Now, e.price);
        }
        public override void Read(object _, _DTwsEvents_tickSizeEvent e)
        {
            if (configuration == null) return;
            Add((TickType)e.tickType, Times.Now, e.size);
        }
        public override void Cancel(object _, System.EventArgs __)
        {
            cancel.Set();
        }
        public override bool Do(IReaderConfiguration configuration)
        {
            this.configuration = configuration;
            logger.Info("Start real time reader");
            cancel.WaitOne();
            return true;
        }
    }
}